package com.gateway.service.aop;

import com.alibaba.fastjson.JSON;
import com.gateway.api.util.CallChainContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: shaoyu1
 * @date: 2019/11/5 16:31
 */
@Aspect
@Component
@Slf4j
public class AopAspect {


    @Around(value = "execution(public * com.gateway.service..*.*(..))||execution(public * com.gateway.web..*.*(..))")
    public Object doAround(ProceedingJoinPoint point)throws Throwable{
        Object[] args = point.getArgs();
        Map<String,Object> context = new HashMap<>();
        context.put("className",point.getTarget().getClass().getSimpleName());
        context.put("methodName",point.getSignature().getName());
        context.put("startTime",System.currentTimeMillis());
        dealArgs(context,args);
        Object result = dealResult(context,point.proceed(args));
        String classAndMethod = point.getTarget().getClass().getName()+"#"+context.get("methodName").toString();
//        CallChainContext.addChain(classAndMethod,args,result);
        return result;
    }
    public Object[] dealArgs(Map<String,Object> context,Object[] args){
        //
        return args;
    }
    public Object dealResult(Map<String,Object> context,Object object){
        //
        return object;
    }
}
